package com.xquant.pricing.pricingConfig.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageInfo;
import com.xquant.common.core.constant.ErrMsgConstant;
import com.xquant.common.core.exception.ServiceException;
import com.xquant.pricing.pricingConfig.mapper.EngineConfigMapper;
import com.xquant.pricing.pricingConfig.service.EngineConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Auther: B946
 * @Date: 10/03/2020 15:24
 * @Description:
 */
@Service(EngineConfigService.BEAN_NAME)

public class EngineConfigServiceImpl implements EngineConfigService {

    @Autowired
    private EngineConfigMapper engineConfigMapper;

    @Override
    public PageInfo<Map<String, Object>>  queryEngineConfigByPage(Map<String, Object> params) {
        List<Map<String,Object>> list = null;
        try {
            list = engineConfigMapper.queryEngineConfig(params);
        }catch(SQLException e){
            throw new ServiceException(ErrMsgConstant.QUERY_ERRINFO,e.getMessage());
        }
        List<Map<String, Object>> collect = null;
        Object productType = params.get("productType");
        Object exerciseType = params.get("exerciseType");
        if (ObjectUtil.isNotNull(productType)&&!"CustomOption".equals(productType)){

             collect = list.stream().filter(item -> {
                return ObjectUtil.isNull(item.get("products"))||item.get("products").toString().indexOf(productType.toString())!=-1;
            }).collect(Collectors.toList());
            if (ObjectUtil.isNotNull(exerciseType)&&!"".equals(exerciseType.toString())){
                String exerciseTypeStr = exerciseType.toString();
                if (exerciseTypeStr.equals("European")) {
                    collect =  collect.stream().filter(item ->{
                        return !"BinomialTree".equals(item.get("method").toString());
                    }).collect(Collectors.toList());
                }
            }
        }else {
            return new PageInfo<Map<String,Object>>(list);
        }
        return new PageInfo<Map<String,Object>>(collect);
    }

    public void saveConfig(Map<String, Object> params){
        try{
            List<Map<String,Object>> list = engineConfigMapper.checkEngineConfig(params);
            if(list != null && list.size() > 0){
                throw new ServiceException(ErrMsgConstant.INSERT_SAME_ERRINFO);
            }
            engineConfigMapper.saveConfig(params);
        }catch(SQLException e){
            throw new ServiceException(ErrMsgConstant.UPDATE_ERRINFO,e.getMessage());
        }
    }
    public void updateConfig(Map<String, Object> params){
        try{
            engineConfigMapper.updateConfig(params);
        }catch(SQLException e){
            throw new ServiceException(ErrMsgConstant.UPDATE_ERRINFO,e.getMessage());
        }
    }
    public void deleteConfigs(Map<String, Object> params){
        try {
            engineConfigMapper.deleteConfigs(params);
        }catch(SQLException e){
            throw new ServiceException(ErrMsgConstant.DELETE_ERRINFO,e.getMessage());
        }

    }

    @Override
    public int queryContractCfgCountByPPCode(String ppCode) {
        try {
          return engineConfigMapper.queryContractCfgCountByPPCode(ppCode);
        }catch(SQLException e){
            throw new ServiceException(ErrMsgConstant.QUERY_ERRINFO,e.getMessage());
        }
    }
}
